
$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'fun/emp/list',
        datatype: "json",
        colModel: [
            { label: '员工ID', name: 'empId', width: 30, key: true},
            { label: '姓名', name: 'empName', width: 100 },
            { label: '身份证号', name: 'idCard', width: 100 },
            { label: '所属部门', name: 'deptName', width: 100 },
            { label: '职名（务）', name: 'office',  width: 100 },
            { label: '状态', name: 'status', width: 50, formatter: function(value, options, row){
                    return value === 0 ?
                        '<span >在岗</span>' :
                        '<span >不在岗</span>';
                }},
            { label: '备注', name: 'remarks',  width: 100 }
        ],
        viewrecords: true,
        height: 400,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("#jqGrid").setGridParam().hideCol("empId").trigger("reloadGrid");
        },
    });
});
//部门结构树
var dept_ztree;
var dept_setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        },

    }
};

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        pickerOptions: {
            disabledDate(time) {
                return time.getTime() > Date.now();
            }
        },
        emp:{},
        dialogFormVisible: false,
        dialogDeptVisible: false,
        dialog: false,
        drawer: false,
        formLabelWidth: '100px',
        timer: null,
        queryByDate:[],
        dbConnect:
            {
                ip:null,
                root:null,
                password:null,
                dbType:null,
            },
        dataType:[{value:'mysql'},{value:'sqlserver'}],
    },

    methods: {
        query: function () {
            vm.reload();
        },
        openDialog:function () {
            var empIds = getSelectedRows();
            if (empIds == null) {
                return;
            }
            vm.dialogFormVisible=true;
        },
        saveDataToDB:function ()
        {
            var empIds = getSelectedRows();
            $.ajax({
                type: "POST",
                url: baseURL + "fun/emp/dbConnect",
                contentType: "application/json",
                data: JSON.stringify({'connectDB':vm.dbConnect,'empIds':empIds}),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.dialogFormVisible=false;
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        saveToMq:function ()
        {
            var empIds = getSelectedRows();
            if (empIds == null) {
                return;
            }
            $.ajax({
                type: "POST",
                url: baseURL +"fun/emp/saveDataToMq",
                contentType: "application/json",
                data: JSON.stringify(empIds),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function (index) {
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        handleCloseDrawer:function(done) {
            vm.dialog=false;
        },
        cancelForm:function() {
            vm.dialog = false;
        },
        handlePreview:function(file) {
            console.log(file);
        },
        handleRemove:function(file, fileList) {
            console.log(file, fileList);
        },
        handleChange(val) {
            console.log(val);
        },
        beforeRemove(file, fileList) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        },
        fresh:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogFormVisible=false;
        },
        freshDept:function (event)
        {
            $("#jqGrid").trigger("reloadGrid");
            vm.dialogDeptVisible=false;
        },
        getInfo: function(empId){
            $.get(baseURL + "fun/emp/info/"+empId, function(r){
                vm.emp = r.emp;
            });
        },
        getDept: function(){
            //加载部门树
            $.get(baseURL + "sys/dept/list", function(r){
                dept_ztree = $.fn.zTree.init($("#deptTree"), dept_setting , r);
            })
        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = dept_ztree.getSelectedNodes();
                    //选择上级部门
                    vm.emp.deptId = node[0].deptId;
                    vm.emp.deptName = node[0].name;
                    layer.close(index);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            if(vm.queryByDate==null)
            {
                vm.queryByDate=['',''];
            }
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1],'deptId':''},
                page:page
            }).trigger("reloadGrid");
        }
    }
});
window.addEventListener("message",function(event){
    vm.showList = true;
    if(vm.queryByDate==null)
    {
        vm.queryByDate=['',''];
    }
    var page = $("#jqGrid").jqGrid('getGridParam','page');
    $("#jqGrid").jqGrid('setGridParam',{
        postData:{'deptId':event.data,'dateStart': vm.queryByDate[0],'dateEnd': vm.queryByDate[1]},
        page:page
    }).trigger("reloadGrid");
}, false);